﻿/*
* Activity Tracking Log v0.1.1 - http://activitytrackinglog.codeplex.com
* Activity Tracking Log for ASP.Net & ASP.Net MVC
* Copyright (c) 2011 Vadim Burac
* Licensed under the Microsoft Public License (Ms-PL)
* http://activitytrackinglog.codeplex.com/license
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace ActivityTrackingLog.Utils
{
    internal class ResourceHelper
    {
        private static object _syncObj = new object();
        private static Dictionary<string, string> _cachedContentFiles = null;
        // list report content
        private static readonly string _listLayoutStylesPath = "ActivityTrackingLog.Web.Static.Styles.ListLayout.css";
        private static readonly string _listThemeStylesPath = "ActivityTrackingLog.Web.Static.Styles.ListTheme.css";
        private static readonly string _listPagePath = "ActivityTrackingLog.Web.Static.Views.ListPage.html";
        private static readonly string _listControlPath = "ActivityTrackingLog.Web.Static.Views.ListControl.html";
        private static readonly string _chartPagePath = "ActivityTrackingLog.Web.Static.Views.ChartPage.html";
        private static readonly string _chartControlPath = "ActivityTrackingLog.Web.Static.Views.ChartControl.html";
        // js
        private static readonly string _clientLogProxyPath = "ActivityTrackingLog.Web.Static.Js.ActivityTrackingLogProxy.js";

        #region ctors

        static ResourceHelper()
        {
            _cachedContentFiles = new Dictionary<string, string>();
        }

        #endregion

        #region list report resources

        internal static string GetListLayoutStyles()
        {
            return GetResourceStringBody(_listLayoutStylesPath);
        }

        internal static string GetListThemeStyles()
        {
            return GetResourceStringBody(_listThemeStylesPath);
        }

        internal static string GetListPageHtml()
        {
            return GetResourceStringBody(_listPagePath);
        }

        internal static string GetListControlHtml()
        {
            return GetResourceStringBody(_listControlPath);
        }

        #endregion

        #region chart report resources

        internal static string GetChartControlHtml()
        {
            return GetResourceStringBody(_chartControlPath);
        }

        internal static string GetChartPageHtml()
        {
            return GetResourceStringBody(_chartPagePath);
        }

        #endregion

        #region js resources

        internal static string GetClientLogProxyJs()
        {
            return GetResourceStringBody(_clientLogProxyPath);
        }

        #endregion

        #region helpers

        private static string ReadStreamToString(Stream stream)
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }

        private static byte[] ReadStreamToByteArr(Stream stream)
        {
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, (int)stream.Length);

            return buffer;
        }

        private static string GetResourceStringBody(string resourcePath)
        {
            lock (_syncObj)
            {
                if (_cachedContentFiles.ContainsKey(resourcePath))
                    return _cachedContentFiles[resourcePath];
            }

            var content = ReadStreamToString(GetResourceStream(resourcePath));
            lock (_syncObj)
            {
                if (!_cachedContentFiles.ContainsKey(resourcePath))
                    _cachedContentFiles.Add(resourcePath, content);
            }

            return content;
        }

        private static byte[] GetResourceBinaryBody(string resourcePath)
        {
            return ReadStreamToByteArr(GetResourceStream(resourcePath));
        }

        private static Stream GetResourceStream(string resourcePath)
        {
            Assembly current = Assembly.GetExecutingAssembly();
            return current.GetManifestResourceStream(resourcePath);
        }

        #endregion
    }
}
